﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Messaging;
using LiSAClient.Core;
using LiSAClient.Core.Interfaces;
using LiSAClient.Core.Messages;
using LiSAClient.Core.Models;
using LiSAClient.Core.Services;
using LiSAClient.Core.Services.Factories;
using LiSAClient.Core.Viewmodel;
using LiSAClient.LiSAService;

namespace LiSAClient.WpfClient.ViewModel.Projects.Edit
{
    class EditQuestionsViewModel: ContentViewModel, ICommitable
    {
        public ICommand Commit { get; private set; }
        public ICommand AddNewResearchQuestion { get; private set; }


        private Project project;
        public Project Project
        {
            get
            {
                return this.project;
            }

            set
            {
                this.Set(() => this.Project, ref this.project, value);
            }
        }


        private ResearchQuestion newResearchQuestion;
        public ResearchQuestion NewResearchQuestion
        {
            get
            {
                return this.newResearchQuestion;
            }

            set
            {
                this.Set(() => this.NewResearchQuestion, ref this.newResearchQuestion, value);
            }
        }

        private ResearchQuestion selectedResearchQuestion;
        public ResearchQuestion SelectedResearchQuestion
        {
            get
            {
                return this.selectedResearchQuestion;
            }

            set
            {
                this.Set(() => this.SelectedResearchQuestion, ref this.selectedResearchQuestion, value);
            }
        }

        #region Initializers

        public EditQuestionsViewModel(Project proj)
        {
            this.Project = proj;
            this.Commit = new RelayCommand(UpdateProject);
            this.AddNewResearchQuestion = new RelayCommand(DoAddNewResearchQuestion);
            this.NewResearchQuestion = new ResearchQuestion();
            this.SelectedResearchQuestion = new ResearchQuestion();
        }
        #endregion

        #region methods

        public void DoAddNewResearchQuestion()
        {
            if (string.IsNullOrWhiteSpace(this.NewResearchQuestion.Question) ||
                string.IsNullOrWhiteSpace(this.NewResearchQuestion.QuestionID))
            {

                Messenger.Default.Send(new ShowNotificationMessage
                {
                    Header = "Missing Information",
                    Body = "You msut have a question ID and text to add a new question. Rational is optional.",
                    NotificationType = NotificationType.Warning
                });
                return;
            }
            var newResearchQs = new List<ResearchQuestion>(this.Project.ResearchQuestions) {NewResearchQuestion};
            this.Project.ResearchQuestions = newResearchQs;
            this.NewResearchQuestion = new ResearchQuestion
            {
                Question = string.Empty,
                QuestionID = string.Empty,
                Rational = string.Empty
            };
        }

        /// <summary>
        /// Save the project and close the window
        /// </summary>
        public void UpdateProject()
        {
            MessageBoxResult messageBoxResult = System.Windows.MessageBox.Show("Update Project?", "Update Confirmation", System.Windows.MessageBoxButton.YesNo);
            if (messageBoxResult == MessageBoxResult.Yes)
            {
                ServiceFactory.Instance.Get<ProjectService>().UpdateProject(this.Project);

                // Close the window
                if (this.Presenter != null)
                {
                    this.Presenter.SendClosingRequest();
                }
            }
        }
        #endregion
    }
}
